package com.redteam.data.dao;

import com.redteam.data.beans.Airplane;
import com.redteam.data.dao.interfaces.DAO;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

/**
 * DAO for retrieving <code>Airplanes</code>
 * @author Jezreel Wilson
 * @version 1.0
 */
public class AirplaneDAO extends AbstractDAO implements DAO {

    private static AirplaneDAO dao;
    private static DataSource ds;
    private final String tableName;

    static {
        dao = new AirplaneDAO();
    }

    private AirplaneDAO() {
        getDataSource();
        tableName = "HIGHFLIERS.RentalItems";
    }

    /**
     * Static method for receiving <code>AirplaneDAO</code>
     * @return
     * AirplaneDAO
     */
    public static AirplaneDAO getInstance() {
        return dao;
    }

    private void getDataSource() {
        try {
            InitialContext ic = new InitialContext();
            ds = (DataSource) ic.lookup("jdbc/redteam");

        } catch (NamingException ex) {
            Logger.getLogger(AirplaneDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * (@inherentDoc)
     */
    @Override
    public String buildUpdateQuery(Object obj) {
        Airplane airplane = (Airplane) obj;
        StringBuilder sb = new StringBuilder("UPDATE ");
        sb.append(getTableName()).append(" SET HOBBS_LEFT_ENGINE = ").append(airplane.getHobbsLeftEngine());
        sb.append(", HOBBS_LAST_OVERHAUL = ").append(airplane.getHobbsLastOverhaul());
        sb.append(", DATE_LAST_OVERHAUL = '").append(airplane.getDateLastOverhaul());
        sb.append("', ANNUAL_DATE = '").append(airplane.getAnnualDate());
        sb.append("', HOBBS_LAST_HUNDRED = ").append(airplane.getHobbsLastHundred());
        sb.append(", DATE_LAST_HUNDRED = '").append(airplane.getDateLastHundred());
        sb.append("', CURRENT_FUEL = ").append(airplane.getCurrentFuel());
        sb.append(", CURRENT_OIL = ").append(airplane.getCurrentOil());
        sb.append(", RATE = ").append(airplane.getRate());
        sb.append(", TAKEOFFS = ").append(airplane.getTakeOffs());
        sb.append(", LANDINGS = ").append(airplane.getLandings());
        sb.append(" WHERE TAILNUMBER = '").append(airplane.getTailNumber()).append("'");
        return sb.toString();
    }

    /**
     * (@inherentDoc)
     */
    @Override
    public Airplane getObjectOut(ResultSet rs) {
        Airplane airplane = new Airplane();
        Long planeId = null;
        try {
            planeId = rs.getLong("ID");
            airplane.setId(planeId);
            airplane.setFboID(rs.getLong("FBO_ID"));
            airplane.setRate(rs.getDouble("RATE"));
            airplane.setHobbsLeftEngine(rs.getDouble("HOBBS_LEFT_ENGINE"));
            airplane.setHobbsLastOverhaul(rs.getDouble("HOBBS_LAST_OVERHAUL"));
            airplane.setHobbsLastHundred(rs.getDouble("HOBBS_LAST_HUNDRED"));
            airplane.setUsableFuel(rs.getDouble("USABLE_FUEL"));
            airplane.setCurrentFuel(rs.getDouble("CURRENT_FUEL"));
            airplane.setOilCapacity(rs.getDouble("OILCAPACITY"));
            airplane.setCurrentOil(rs.getDouble("CURRENT_OIL"));
            airplane.setFuelGrade(rs.getString("FUELGRADE"));
            airplane.setOilGrade(rs.getString("OILGRADE"));
            airplane.setTailNumber(rs.getString("TAILNUMBER"));
            airplane.setManufacturer(rs.getString("MANUFACTURER"));
            airplane.setModel(rs.getString("MODEL"));
            airplane.setDateLastOverhaul(rs.getDate("DATE_LAST_OVERHAUL"));
            airplane.setAnnualDate(rs.getDate("ANNUAL_DATE"));
            airplane.setDateLastHundred(rs.getDate("DATE_LAST_HUNDRED"));
            airplane.setAircraftType(rs.getString("TYPE"));
            airplane.setType(rs.getString("DISCRIMINATOR"));
            airplane.setTakeOffs(rs.getInt("TAKEOFFS"));
            airplane.setLandings(rs.getInt("LANDINGS"));
            airplane.setImageLocation(rs.getString("IMAGE_LOCATION"));

            String endorsements = rs.getString("ENDORSEMENTS");
            String[] endorsementArr = endorsements.split(",");
            List<String> endorsementList = new ArrayList();
            endorsementList.addAll(Arrays.asList(endorsementArr));
            airplane.setEndorsements(endorsementList);
            airplane.setStringDates(airplane.getDateLastHundred(), airplane.getAnnualDate(), airplane.getDateLastHundred());
        } catch (SQLException ex) {
            Logger.getLogger(AirplaneDAO.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            return airplane;
        }
    }

    /**
     * (@inherentDoc)
     */
    @Override
    public String buildInsertQuery(Object obj) {
        Airplane airplane = (Airplane) obj;
        StringBuilder sb = new StringBuilder("INSERT INTO ").append(this.getTableName());
        sb.append("(ID,FBO_ID,RATE,HOBBS_LEFT_ENGINE,HOBBS_LAST_OVERHAUL,HOBBS_LAST_HUNDRED");
        sb.append("USABLE_FUEL,CURRENT_FUEL,OILCAPACITY,CURRENT_OIL,FUELGRADE,OILGRADE");
        sb.append("TAILNUMBER,MANUFACTURER,MODEL,DATE_LAST_OVERHAUL,ANNUAL_DATE,DATE_LAST_HUNDRED,");
        sb.append("TYPE,TAKEOFFS,LANDINGS,DISCRIMINATOR,IMAGE_LOCATION) VALUES (").append(airplane.getFboID()).append(",").append(airplane.getRate()).append(",");
        sb.append(airplane.getHobbsLeftEngine()).append(",").append(airplane.getHobbsLastOverhaul()).append(",");
        sb.append(airplane.getHobbsLastHundred()).append(",").append(airplane.getUsableFuel()).append(",");
        sb.append(airplane.getCurrentFuel()).append(",").append(airplane.getOilCapacity()).append("',");
        sb.append(airplane.getCurrentOil()).append(",'").append(airplane.getFuelGrade()).append("','");
        sb.append(airplane.getOilGrade()).append("','").append(airplane.getTailNumber()).append("','");
        sb.append(airplane.getManufacturer()).append("','").append(airplane.getModel()).append("','");
        sb.append(airplane.getDateLastOverhaul()).append("','").append(airplane.getAnnualDate()).append("','");
        sb.append(airplane.getDateLastHundred()).append("','").append(airplane.getAircraftType()).append("',");
        sb.append(airplane.getTakeOffs()).append(",").append(airplane.getLandings()).append(",'");
        sb.append(airplane.getType()).append("','").append(airplane.getImageLocation()).append("')");
        return sb.toString();
    }

    /**
     * (@inherentDoc)
     */
    @Override
    public long getObjectId(Object o) {
        Airplane airplane = (Airplane) o;
        return airplane.getId();
    }

    /**
     * (@inherentDoc)
     */
    @Override
    public DataSource getDS() {
        return ds;
    }

    /**
     * (@inherentDoc)
     */
    @Override
    public String getTableName() {
        return tableName;
    }

    /**
     * (@inherentDoc)
     */
    @Override
    public List<Object> getAll() throws SQLException {
        return getAll("airplane");
    }

    /**
     * (@inherentDoc)
     */
    @Override
    public Object getSingle(long id) throws SQLException {
        return getSingle(id, "airplane");
    }
}
